package com.prs.crm.dao.cdxh.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.cdxh.SaleOrderDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.cdxh.SaleOrder;
import com.prs.crm.domain.cdxh.SaleOrderDetail;
import com.prs.crm.domain.cdxh.cd_Product;

public class SaleOrderDaoImpl extends BaseDaoImpl<SaleOrder> implements
		SaleOrderDao {

	@Override
	public SaleOrder getSaleOrder(Serializable id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(SaleOrder.class, id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public SaleOrderDetail getSaleOrderDetail(final String productCode,
			final SaleOrder aleOrder) {
		return (SaleOrderDetail) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(SaleOrderDetail.class);
						criteria.add(Restrictions
								.eq("productCode", productCode));
						criteria.add(Restrictions.eq("saleOrder", aleOrder));
						List<SaleOrderDetail> result = criteria.list();
						if (result.size() == 0) {
							return null;
						} else {
							return result.get(0);
						}
					}
				});
	}

	@Override
	public void saveOrudetail(SaleOrderDetail saleOrderDetail) {
		this.getHibernateTemplate().saveOrUpdate(saleOrderDetail);

	}

	@Override
	public void saveOrudsale(SaleOrder saleOrder) {
		this.getHibernateTemplate().saveOrUpdate(saleOrder);
	}

	@SuppressWarnings("unchecked")
	@Override
	public SaleOrder getSaleOrder(final String orders) {
		return (SaleOrder) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(SaleOrder.class);
						criteria.add(Restrictions.eq("saleOrderCode", orders));
						List<SaleOrder> result = criteria.list();
						if (result.size() == 0) {
							return null;
						} else {
							return result.get(0);
						}
					}
				});
	}

	@Override
	public void removeSaleOrder(SaleOrder so) {
		this.getHibernateTemplate().delete(so);

	}

	@Override
	public void removeSaleOrderDetail(SaleOrderDetail sod) {
		this.getHibernateTemplate().delete(sod);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<SaleOrderDetail> getSaleOrderDetails(final SaleOrder saleOrders) {
		return (List<SaleOrderDetail>) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(SaleOrderDetail.class);
						criteria.add(Restrictions.eq("saleOrder", saleOrders));
						List<SaleOrderDetail> result = criteria.list();
						
							return result;
						
					}
				});
	}

	@Override
	public SaleOrderDetail getSaleOrderDetail(final String productcode) {
		return (SaleOrderDetail) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session.createCriteria(SaleOrderDetail.class);
						criteria.add(Restrictions.eq("salesCodeN", productcode));
						List<SaleOrder> result = criteria.list();
						if (result.size() == 0) {
							return null;
						} else {
							return result.get(0);
						}
					}
				});
	}

	@Override
	public SaleOrderDetail getSaleOrderDetailByProduct(final cd_Product product) {
		return (SaleOrderDetail) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session.createCriteria(SaleOrderDetail.class);
						criteria.add(Restrictions.eq("productCode", product.getProductcode()));
						criteria.add(Restrictions.eq("productColor", product.getProductColor()));
						criteria.add(Restrictions.eq("productName", product.getName()));
						criteria.add(Restrictions.eq("productNorm", product.getNorm()));
						List<SaleOrder> result = criteria.list();
						if (result.size() == 0) {
							return null;
						} else {
							return result.get(0);
						}
					}
				});
	}

}
